package com.cmonbaby.adapter.recycleview.wrapper;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.cmonbaby.adapter.recycleview.RViewHolder;
import com.cmonbaby.adapter.recycleview.utils.WrapperUtils;

/**
 * RecycleView空数据包装类
 * @author simon 2016-9-19 下午16:05:29
 * @since JDK1.8 SDK6.0.1
 * @version V2.3.4
 */
public class EmptyWrapper extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int ITEM_TYPE_EMPTY = Integer.MAX_VALUE - 1;
    private RecyclerView.Adapter mInnerAdapter;
    private View mEmptyView; // 空布局
    private int mEmptyLayoutId; // 空布局ID

    /**
     * 构造方法，传递RecyclerView.Adapter
     *
     * @param adapter RecyclerView.Adapter
     */
    public EmptyWrapper(RecyclerView.Adapter adapter) {
        mInnerAdapter = adapter;
    }

    /** 是否为空，当布局为空或者adapter条目为0 */
    private boolean isEmpty() {
        return (mEmptyView != null || mEmptyLayoutId != 0) && mInnerAdapter.getItemCount() == 0;
    }

    /** 为Item创建视图 */
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (isEmpty()) {
            RViewHolder holder;
            // 获得RecycleViewViewHolder对象，其中使用构造方法实现
            if (mEmptyView != null) {
                holder = RViewHolder.createViewHolder(parent.getContext(), mEmptyView);
            } else {
                // 不考虑空值，为0
                holder = RViewHolder.createViewHolder(parent.getContext(), parent, mEmptyLayoutId);
            }
            return holder;
        }
        // 如果不是空布局，正常显示
        return mInnerAdapter.onCreateViewHolder(parent, viewType);
    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        WrapperUtils.onAttachedToRecyclerView(mInnerAdapter, recyclerView, (gridLayoutManager, oldLookup, position) -> {
            if (isEmpty()) {
                return gridLayoutManager.getSpanCount();
            }
            if (oldLookup != null) {
                return oldLookup.getSpanSize(position);
            }
            return 1;
        });
    }

    @Override
    public void onViewAttachedToWindow(@NonNull RecyclerView.ViewHolder holder) {
        mInnerAdapter.onViewAttachedToWindow(holder);
        if (isEmpty()) WrapperUtils.setFullSpan(holder);
    }

    /** 根据数据源的position返回需要显示的的layout的type，type的值必须从0开始 */
    @Override
    public int getItemViewType(int position) {
        if (isEmpty()) {
            return ITEM_TYPE_EMPTY;
        }
        return mInnerAdapter.getItemViewType(position);
    }

    /** 将数据绑定到Item的视图上 */
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (isEmpty()) return;
        mInnerAdapter.onBindViewHolder(holder, position);
    }

    /** 绘制RecycleView之前，将会先调用getItemCount方法来获取Item的个数 */
    @Override
    public int getItemCount() {
        if (isEmpty()) return 1;
        return mInnerAdapter.getItemCount();
    }

    /**
     * 设置空布局View
     *
     * @param emptyView 空布局View
     */
    public void setEmptyView(View emptyView) {
        mEmptyView = emptyView;
    }

    /**
     * 设置空布局View
     *
     * @param layoutId 空布局ViewID
     */
    public void setEmptyView(int layoutId) {
        mEmptyLayoutId = layoutId;
    }

}
